package com.ykw.parking.controller;

import com.ykw.parking.Utils.SnowFlake;
import com.ykw.parking.mapper.*;
import com.ykw.parking.pojo1.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Controller
public class shoppingCartController {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ShoppingCartMapper cartMapper;
    @Autowired
    ParkingMapper parkingMapper;
    @Autowired
    ReserveMapper reserveMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OwnerMapper ownerMapper;
    @RequestMapping("/shopping")
    @ResponseBody
    public List shoppingCart(HttpSession session){
        String  username = (String)session.getAttribute("username");
        System.out.println("username="+username);
        List<ShopCar> parkings=new ArrayList();
        User user = userMapper.queryUserbyname(username);
        System.out.println(user+"----");
        if(user!=null){
            List<String> parkingIds = cartMapper.queryByUserId(user.getId());//根据用户的id得到购物车中物品的id的集合
            System.out.println(parkingIds);
            for (String pid: parkingIds){
                System.out.println(pid);
                ParkingLot parkingLot = parkingMapper.queryParkingLotById(Integer.valueOf(pid));
                Parking parking = parkingMapper.queryByParkingId(parkingLot.getPid());
                parkings.add(new ShopCar(String.valueOf(parkingLot.getId()),
                        parkingLot.getPid(),
                        parkingLot.getPrice(),
                        parking.getCity()+parking.getAddress(),
                        parkingLot.getNumber()));
            }
            System.out.println(parkings);
            return parkings;
        }
            return parkings;
    }
    @RequestMapping(value = "/addShoppingCart",method = RequestMethod.POST)
    @ResponseBody
    public String addShopping(String parkingId,HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);

        RealUser realUser = orderMapper.queryUser(user.getId());
        if(realUser==null){
            return "请先实名";
        }
        UserOwner userOwner = ownerMapper.queryOwnerByUid(user.getId());
        if (userOwner==null){
            return "请完成业主认证";
        }
        String s = cartMapper.queryByParkingId(user.getId(), parkingId);
        UserOrder userOrder = orderMapper.queryOrder(user.getId(), Integer.valueOf(parkingId));
        if(user!=null){
            if(s!=null){
                return "商品已存在购物车中";
            }
            if(userOrder!=null){
                return "商品已在您的订单中";
            }
            String userId = user.getId();
            cartMapper.insertUserIdAndParkingId(userId,parkingId);
            return "加入购物车成功";
        }else return "加入购物车失败";
    }
    @RequestMapping(value = "/reserve")
    @ResponseBody
    public String reserve(@RequestParam(value = "reserveList[]") List<String> reserveList,HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        System.out.println(reserveList);
        Iterator iterator = reserveList.iterator();
        while (iterator.hasNext()) {
            String next =(String)iterator.next();
            parkingMapper.setReserve(next,"1");
            reserveMapper.insertReserve(user.getId(),next);
        }
        return "1";
    }
    @RequestMapping(value ="/deleteCart")
    @ResponseBody
    public String deleteCart(@RequestParam(value = "deleteCartList[]") List<String> deleteCartList,HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        Iterator<String> iterator = deleteCartList.iterator();
        while (iterator.hasNext()) {
            String next =  iterator.next();
            cartMapper.deleteCartByParkingId(next,user.getId());
        }
        return "1";
    }
    @RequestMapping(value ="/jiezhang",method = RequestMethod.POST)
    @ResponseBody
    public String jiezhang(@RequestParam("jz[]") List<Integer> jz,@RequestParam("ok") Integer ok,@RequestParam("monry")Double monry,HttpSession session){

        System.out.println(jz);
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        RealUser realUser = orderMapper.queryUser(user.getId());
        Double money = userMapper.getMoney(user.getId());
        if(money<monry){
            return "1";
        }
        for (Integer order:jz){
            ParkingLot parkingLot = orderMapper.getParkingLot(order);
            Parking parking = orderMapper.getParking(Integer.valueOf(parkingLot.getPid()));

            UserOrder userOrder=new UserOrder(new SnowFlake(2,3).nextId(),user.getId(),new Date(System.currentTimeMillis()),
                    parking.getId(),parkingLot.getNumber(),parking.getCity(),parking.getAddress(),ok,
                    parkingLot.getPrice(),parkingLot.getId());
            int i = orderMapper.addOrder(userOrder);
            if(i>0){
                if(ok==1){
                    userMapper.addMoney(money-monry,user.getId());
                    orderMapper.updateParkingLot(ok,parkingLot.getId());
                    int i1 = orderMapper.updateOrder(userOrder.getOid(), parkingLot.getId());
                    orderMapper.addUserPaking(new UserParking(user.getId(),parking.getId(),parkingLot.getId(),parking.getCity(),
                            parking.getAddress(),parkingLot.getNumber(),realUser.getName(),realUser.getId_number(),realUser.getPhone_number()));
                    cartMapper.deleteCartByPId(String.valueOf(parkingLot.getId()));
                    SimpleDateFormat sdf=new SimpleDateFormat("MM-dd");
                    java.util.Date date=new java.util.Date();
                    String str=sdf.format(date);
                    redisTemplate.opsForValue().increment(str);
                }
                cartMapper.deleteCartByParkingId(String.valueOf(parkingLot.getId()),user.getId());
            }
        }
        return "0";
    }

    @RequestMapping(value ="/test",method = RequestMethod.POST)
    @ResponseBody
    public String test(){
        System.out.println("test");
        return null;
    }
}
